#pragma once

#include <assert.h>

template <class T> class c_singleton 
{
public:
    c_singleton()
    {
        assert(!ms_singleton);    
#if defined( _MSC_VER ) && _MSC_VER < 1200	 
        int offset = (int)(T*)1 - (int)(c_singleton<T>*)(T*)1;
        ms_singleton = (T*)((int)this + offset);
#else   
        ms_singleton = static_cast< T* >( this );
#endif 
    }
    
    ~c_singleton( void )
    {  assert( ms_singleton );  ms_singleton = 0;  }
    static T& get_singleton( void )
    {	assert( ms_singleton );  return ( *ms_singleton ); }
    static T* get_singleton_ptr( void )
    { return ms_singleton; }

protected:
    static T *ms_singleton; 
    
};

#define IMPLEMENT_SINGLETON(singleton_class) \
    template<> singleton_class* c_singleton<singleton_class>::ms_singleton = NULL; 